# extractSpectra.py
# 4/15/2013, John Kwong
# 
import os
from sys import argv

print len(argv)
script, prefix, fileLocation, datasetNumFirst, datasetNumLast, firstTimeChannel, lastTimeChannel = argv

##prefix = argv[0]
##fileLocation = argv[1]
##firstTimeChannel = argv[2]
##lastTimeChannel = argv[3]

# Get list of files
# dump all the headers into a file
for i in range(int(datasetNumFirst), int(datasetNumLast)+1):
    fileName = prefix + '%02d' % i
    print(fileName)
    # dice extractSpectra_byTimeChannel-FD.cut ../data dy98 0 2 256
    for detectorNumber in range(40):
        print detectorNumber+1
        fullFilename =  (fileLocation + '/' + fileName + '.mar')
        if os.path.isfile(fullFilename):
            os.system('dice extractSpectra_byTimeChannel-FD.cut ' + \
                      fileLocation + ' ' + fileName + ' ' + str(detectorNumber+1) + ' ' + str(firstTimeChannel) + ' ' + str(lastTimeChannel))
            print('dice extractSpectra_byTimeChannel-FD.cut ' + \
                      fileLocation + ' ' + fileName + ' ' + str(detectorNumber+1) + ' ' + str(firstTimeChannel) + ' ' + str(lastTimeChannel))
        else:
            print('File not found: %s' %fullFilename)
            
print('Done.')
